package com.example.db;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;
import com.example.db.bean.User;
import com.example.db.utils.KryoUtil;
import com.example.db.utils.TimeUtils;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;

/**
 * Created by kkmike999 on 16/2/13.
 */
public class KryoTest extends TestCase {

    public static final int COUNT = 10000;

    User[] users = UserHelper.createUserList(COUNT);

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void kryoTime() {
        TimeUtils.begin("kryo Obj");

        for (User user : users) {
            KryoUtil.toBytes(user);
        }

        System.out.println("KryoTest(" + users.length + "): kryo user->string time = " + TimeUtils.time("kryo Obj"));
    }

    @Test
    public void kryoTransaction() {
        TimeUtils.begin("kryo transaction");

        Kryo kryo = new Kryo();
        kryo.register(User.class);

        ByteArrayOutputStream bout   = new ByteArrayOutputStream();
        Output                output = new Output(bout);

        for (User user : users) {
            kryo.writeObject(output, user);
            bout.toByteArray();
            output.clear();
            bout.reset();
        }

        output.close();

        System.out.println("KryoTest(" + users.length + "): kryo transaction time = " + TimeUtils.time("kryo transaction"));
    }
}
